/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.own.entity.OwnMedicineFieldManagement;
import com.jeesite.modules.own.entity.OwnMedicineGrowerInformation;
import com.jeesite.modules.own.entity.OwnMedicineReceiptInformation;
import com.jeesite.modules.own.service.OwnMedicineFieldManagementService;
import com.jeesite.modules.own.service.OwnMedicineGrowerInformationService;
import com.jeesite.modules.own.service.OwnMedicineReceiptInformationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnMedicinePlantInformation;
import com.jeesite.modules.own.service.OwnMedicinePlantInformationService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 种植信息Controller
 * @author F
 * @version 2019-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownMedicinePlantInformation")
public class OwnMedicinePlantInformationController extends BaseController {

	@Autowired
	private OwnMedicinePlantInformationService ownMedicinePlantInformationService;

	@Autowired
	private OwnMedicineGrowerInformationService ownMedicineGrowerInformationService;

	@Autowired
	private OwnMedicineFieldManagementService ownMedicineFieldManagementService;

	@Autowired
	private OwnMedicineReceiptInformationService ownMedicineReceiptInformationService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnMedicinePlantInformation get(String id, boolean isNewRecord) {
		return ownMedicinePlantInformationService.get(id, isNewRecord);
	}

	/**
	 * 获取单条数据
	 * @param ownMedicinePlantInformation
	 * @return
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = "findById")
	public OwnMedicinePlantInformation findById(OwnMedicinePlantInformation ownMedicinePlantInformation){
		return ownMedicinePlantInformationService.get(ownMedicinePlantInformation);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnMedicinePlantInformation ownMedicinePlantInformation, Model model) {
		model.addAttribute("ownMedicinePlantInformation", ownMedicinePlantInformation);
		return "modules/own/ownMedicinePlantInformationList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnMedicinePlantInformation> listData(OwnMedicinePlantInformation ownMedicinePlantInformation, HttpServletRequest request, HttpServletResponse response) {
		ownMedicinePlantInformation.setPage(new Page<>(request, response));
		Page<OwnMedicinePlantInformation> page = ownMedicinePlantInformationService.findPage(ownMedicinePlantInformation);
		return page;
	}
	/**
	 * 查询还在种植中的列表数据
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = "listDataNormal")
	@ResponseBody
	public Page<OwnMedicinePlantInformation> listDataNormal(OwnMedicinePlantInformation ownMedicinePlantInformation, HttpServletRequest request, HttpServletResponse response) {
		ownMedicinePlantInformation.setPage(new Page<>(request, response));
		String[] strs = {"0","1"};
		ownMedicinePlantInformation.setHarvestStatus_in(strs);
		Page<OwnMedicinePlantInformation> page = ownMedicinePlantInformationService.findPage(ownMedicinePlantInformation);
		return page;
	}


	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = "form")
	public String form(OwnMedicinePlantInformation ownMedicinePlantInformation, Model model) {
		model.addAttribute("ownMedicinePlantInformation", ownMedicinePlantInformation);
		return "modules/own/ownMedicinePlantInformationForm";
	}

	/**
	 * 保存种植信息
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnMedicinePlantInformation ownMedicinePlantInformation) {
		if (StringUtils.isBlank(ownMedicinePlantInformation.getBmBatchCode())){
			String plantBmBatchId = UUID.randomUUID().toString();//种植批次码
			ownMedicinePlantInformation.setBmBatchCode(plantBmBatchId); //种植批次码
		}
		ownMedicinePlantInformationService.save(ownMedicinePlantInformation);
		if (ownMedicinePlantInformation.getOwnMedicineGrowerInformationList() != null){
			List<OwnMedicineGrowerInformation> ownMedicineGrowerInformations = ownMedicinePlantInformation.getOwnMedicineGrowerInformationList();
			for (OwnMedicineGrowerInformation ownMedicineGrowerInformation: ownMedicineGrowerInformations){
				ownMedicineGrowerInformation.setOwnMedicinePlantInformationId(ownMedicinePlantInformation.getId());
				ownMedicineGrowerInformation.setBmBatchCode(ownMedicinePlantInformation.getBmBatchCode());
				ownMedicineGrowerInformationService.save(ownMedicineGrowerInformation);
			}
		}
		if (ownMedicinePlantInformation.getOwnMedicineFieldManagementList() != null){
			List<OwnMedicineFieldManagement> ownMedicineFieldManagements = ownMedicinePlantInformation.getOwnMedicineFieldManagementList();
			for (OwnMedicineFieldManagement ownMedicineFieldManagement: ownMedicineFieldManagements){
				ownMedicineFieldManagement.setOwnMedicinePlantInformationId(ownMedicinePlantInformation.getId());
				ownMedicineFieldManagement.setBmBatchCode(ownMedicinePlantInformation.getBmBatchCode());
				ownMedicineFieldManagementService.save(ownMedicineFieldManagement);
			}
		}
		if (ownMedicinePlantInformation.getOwnMedicineReceiptInformationList() != null){
			List<OwnMedicineReceiptInformation> ownMedicineReceiptInformations = ownMedicinePlantInformation.getOwnMedicineReceiptInformationList();
			for (OwnMedicineReceiptInformation ownMedicineReceiptInformation: ownMedicineReceiptInformations){
				String receiptBmBatchId = UUID.randomUUID().toString();
				ownMedicineReceiptInformation.setOwnMedicinePlantInformationId(ownMedicinePlantInformation.getId());
				ownMedicineReceiptInformation.setBmBatchCode(receiptBmBatchId);
				ownMedicineReceiptInformationService.save(ownMedicineReceiptInformation);
			}
		}
		return renderResult(Global.TRUE, text("保存种植信息成功！"), ownMedicinePlantInformation);
	}
	
	/**
	 * 删除种植信息
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnMedicinePlantInformation ownMedicinePlantInformation) {
		ownMedicinePlantInformationService.delete(ownMedicinePlantInformation);
		return renderResult(Global.TRUE, text("删除种植信息成功！"));
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = "findByKey")
	public String plantInfoSelect(OwnMedicinePlantInformation ownMedicinePlantInformation, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (JsonMapper.fromJson(selectDataJson, Map.class) != null){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("plantInformation", ownMedicinePlantInformation);
		return "modules/own/ownMedicinePlantInformationSelect";
	}

	@RequiresPermissions("own:ownMedicinePlantInformation:edit")
	@RequestMapping(value = "updateStatus")
	@ResponseBody
	public String updateStatus(String plantId, String status) {
		OwnMedicinePlantInformation ownMedicinePlantInformation = ownMedicinePlantInformationService.get(plantId);
		ownMedicinePlantInformation.setHarvestStatus(status);
		ownMedicinePlantInformationService.update(ownMedicinePlantInformation);
		return renderResult(Global.TRUE, text("药材收获成功！"));
	}

	/**
	 * 查询种植户、田间管理和收获信息
	 */
	@RequiresPermissions("own:ownMedicinePlantInformation:view")
	@RequestMapping(value = "findGrowerAndFieldAndReceipt")
	@ResponseBody
	public String findGrowerAndFieldAndReceipt(String plantId) {
		Map<String, Object> re = new HashMap<>();
		re.put("grower", ownMedicineGrowerInformationService.findByPlantId(plantId));
		re.put("field", ownMedicineFieldManagementService.findByPlantId(plantId));
		re.put("receipt", ownMedicineReceiptInformationService.findByPlantId(plantId));
		return renderResult(Global.TRUE, text("数据查询成功！"), re);
	}


}